<template>
	<view class="content">
		<view class="header">
			<view class="tabs">
				<view class="item font28" :class="tabsIndex == index?'bg':''" v-for="(item,index) in tabsList"
					@click="tabs(index)">
					{{item.name}}
				</view>
			</view>
			<view class="timeBox">
				<view class="starTime font28" @click="starOpen">
					{{starTime}}
				</view>
				<view class="heng">
					-
				</view>
				<view class="endTime font28" @click="endOpen">
					{{endTime}}
				</view>
			</view>
		</view>
		<view class="box">
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="scrolltolower">
				<view class="item" v-for="(item,index) in profitList" :key="index" v-if="profitList.length > 0">
					<view class="left">
						<view class="title font28">
							{{item.user.nickName}} {{item.user.phone}}
						</view>
						<view class="t_price">
							<view class="type font24">
								{{item.service_goods[0]}}
							</view>
						</view>
					</view>
					<view class="left">
						<view class="title font28">
							会员消费提成<text class="font30" style="color: #FF6600;">￥{{item.discount}}</text>
						</view>
						<view class="t_price">
							<view class="price font30 fontw">
								1次/￥{{item.pay_price}}
							</view>
						</view>
					</view>
				</view>
				<view class="font32 fontw" style="color:#969696;text-align: center;" v-else>
					暂无数据
				</view>
			</scroll-view>
		</view>
		<yt-dateTimePicker ref="starShow" @submit="starTimes" :start-year="2000" :end-year="2099" :time-init="starTime"
			:time-hide="[true, true, true, false, false, false]" :time-label="['年', '月', '日', '时', '分', '秒']" />
		<yt-dateTimePicker ref="endShow" @submit="endTimes" :start-year="2000" :end-year="2099" :time-init="endTime"
			:time-hide="[true, true, true, false, false, false]" :time-label="['年', '月', '日', '时', '分', '秒']" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 0,
				tabList: [{
					name: '保养提成'
				}, {
					name: '充值提成'
				}, {
					name: '会员提成'
				}],
				tabsList: [
					{ name: '今日' },
					{ name: '昨日' },
					{ name: '本月' },
					{ name: '上月' }
				],
				scrollTop: 0,
				tabsIndex: 0,
				starTime: '', //开始时间
				endTime: '', // 结束时间
				profitList: []
			};
		},
		onLoad() {
			this.getCurrentTime()
			this.getData()
		},
		methods: {
			async getData() {
				const data = await this.$api.shareholder.salesRecords({
					start_time: this.starTime,
					end_time: this.endTime,
				})
				this.profitList = data
			},
			getCurrentTime() {
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				const hours = String(now.getHours()).padStart(2, '0');
				const minutes = String(now.getMinutes()).padStart(2, '0');
				const seconds = String(now.getSeconds()).padStart(2, '0');
				this.starTime = `${year}-${month}-${day}`;
				this.endTime = `${year}-${month}-${day}`;
			},
			tabs(index) {
				this.tabsIndex = index
				if (index == 0) {
					this.getCurrentTime()
				}
				if (index == 1) {
					let ty = this.getYesterdayDate().split(' ')
					const yesterday = ty[0]
					this.starTime = yesterday
					this.endTime = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd');
				}
				if (index == 2) {
					const firstDay = this.getFirstDayOfMonth()
					const m_frist = uni.$u.timeFormat(firstDay, 'yyyy-mm-dd');
					this.starTime = m_frist
					this.endTime = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd');
				}
				if (index == 3) {
					const LastMonth = this.getLastMonthDates()
					const f_m = uni.$u.timeFormat(LastMonth.start, 'yyyy-mm-dd');
					const e_m = uni.$u.timeFormat(LastMonth.end, 'yyyy-mm-dd');
					this.starTime = f_m
					this.endTime = e_m
				}
				this.getData()
			},
			starOpen() {
				this.tabsIndex = 4
				this.$refs.starShow.show();
			},
			endOpen() {
				this.tabsIndex = 4
				this.$refs.endShow.show();
			},
			starClose() {
				this.tabsIndex = 0
				this.getCurrentTime()
				this.getData()
				this.$refs.starShow.hide();
			},
			endClose() {
				this.tabsIndex = 0
				this.getCurrentTime()
				this.getData()
				this.$refs.endShow.hide();
			},
			// 开始时间
			starTimes(e) {
				this.starTime = `${e.year}-${e.month}-${e.day}`;
				this.getData()
			},
			// 结束时间
			endTimes(e) {
				this.endTime = `${e.year}-${e.month}-${e.day}`;
				this.getData()
			},
			tabsClick(e) {
				this.current = e.index
			},
			getYesterdayDate() {
				const now = new Date();
				const startTime = new Date(now.getTime() - 24 * 60 * 60 * 1000);
				const startTimeStr = startTime.toISOString().split('T')[0] + ' ' + startTime.toTimeString().split(' ')[0];
				return startTimeStr;
			},
			getFirstDayOfMonth() {
				const now = new Date();
				const month = now.setDate(1);
				const time = now.setHours(0, 0, 0, 0);
				return time
			},
			getLastMonthDates() {
				const now = new Date();
				const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
				const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 0);
				const firstTimestamp = firstDayOfMonth.getTime();
				const lastTimestamp = lastDayOfMonth.getTime();
				return {
					start: firstTimestamp,
					end: lastTimestamp
				};
			},
			scrolltolower() {}
		}
	}
</script>

<style lang="scss">
	.content {
		.header {
			padding: 30rpx 33rpx;
			margin-bottom: 16rpx;
			background-color: #fff;

			.tabs {
				background: #E6F9EF;
				border-radius: 60rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.bg {
					background: #1EB481;
					border-radius: 60rpx;
					color: #fff !important;
				}

				.item {
					width: 25%;
					color: #1EB481;
					text-align: center;
					padding: 20rpx 0;
					transition: 0.3s linear;
				}
			}

			.timeBox {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 32rpx 21rpx;
				background-color: #fff;
				margin-top: 18rpx;

				.starTime {
					color: #333333;
				}

				.heng {}

				.endTime {
					color: #333333;
				}
			}
		}

		.box {
			padding: 0 33rpx;

			.scroll-Y {
				height: calc(100vh - 184rpx - 58rpx - 24rpx);

				.item {
					margin-bottom: 18rpx;
					border-radius: 16rpx;
					overflow: hidden;

					.left {
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding: 47rpx 30rpx 47rpx 20rpx;
						background-color: #fff;

						.title {}

						.t_price {
							display: flex;
							align-items: center;

							.type {
								padding: 7rpx 14rpx;
								background: #F2FDF8;
								border-radius: 50rpx;
								border: 1px solid #7EDBAA;
								color: #07C160;
								margin-right: 8rpx;
							}

							.des {
								color: #333333;
							}

							.price {
								margin: 0 20rpx;
							}

							.icon {
								width: 13rpx;
								height: 24rpx;
								display: flex;

								image {
									width: 13rpx;
									height: 24rpx;
								}
							}
						}

					}
				}
			}
		}
	}
</style>